//
//  mm_subViewForPicRemarksInGetJobSheet.m
//  HondaPro_2
//
//  Created by Eric on 13-7-10.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "mm_subViewForPicRemarksInGetJobSheet.h"
#import "mm_outCheckDisUserDefinedCell.h"

@implementation mm_subViewForPicRemarksInGetJobSheet

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        [self createStaticLbls];
        [self createTableViewDis];
    }
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    
    [self freshenViewDis];
}

//创建静态控件
-(void)createStaticLbls
{
    NSArray *arrForKeys = [NSArray arrayWithObjects:
                           @"bottomLbl",
                           MM_OUT_CHECK_OUT_CHECK_NUMBER_KEY_STR_,
                           MM_OUT_CHECK_CHECK_ITEM_KEY_STR_,
                           MM_OUT_CHECK_DEFECT_COUNT_KEY_STR_,
                           MM_OUT_CHECK_DEFECT_DESCRIPTION_KEY_STR_, nil];
    
    NSDictionary *dicForLblsRect = [NSDictionary dictionaryWithObjectsAndKeys:
                                    [NSValue valueWithCGRect:CGRectMake(0, 0, self.frame.size.width, 50)],@"bottomLbl",
                                    [NSValue valueWithCGRect:CGRectMake(0, 0, 80, 50)],MM_OUT_CHECK_OUT_CHECK_NUMBER_KEY_STR_,
                                    [NSValue valueWithCGRect:CGRectMake(81, 0, 80, 50)],MM_OUT_CHECK_CHECK_ITEM_KEY_STR_,
                                    [NSValue valueWithCGRect:CGRectMake(162, 0, 80, 50)],MM_OUT_CHECK_DEFECT_COUNT_KEY_STR_,
                                    [NSValue valueWithCGRect:CGRectMake(243, 0, self.frame.size.width-243, 50)],MM_OUT_CHECK_DEFECT_DESCRIPTION_KEY_STR_, nil];
    
    NSDictionary *dicForLblsTxt = [NSDictionary dictionaryWithObjectsAndKeys:
                                   @"",@"bottomLbl",
                                   @"编号",MM_OUT_CHECK_OUT_CHECK_NUMBER_KEY_STR_,
                                   @"类型",MM_OUT_CHECK_CHECK_ITEM_KEY_STR_,
                                   @"数量",MM_OUT_CHECK_DEFECT_COUNT_KEY_STR_,
                                   @"备注",MM_OUT_CHECK_DEFECT_DESCRIPTION_KEY_STR_, nil];
    
    for (int i = 0; i<[arrForKeys count]; i++)
    {
        NSString *strKey = [arrForKeys objectAtIndex:i];
        CGRect rect = (CGRect)[[dicForLblsRect objectForKey:strKey] CGRectValue];
        UILabel *lbl = [[UILabel alloc] initWithFrame:rect];
        lbl.text = [dicForLblsTxt objectForKey:strKey];
        lbl.textColor = [UIColor grayColor];
        lbl.font = [UIFont systemFontOfSize:16];
        if ([strKey isEqualToString:@"bottomLbl"])
        {
            lbl.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
        }
        else
        {
            lbl.backgroundColor = [UIColor clearColor];
        }
        [self addSubview:lbl];
    }
}

//创建表单
-(void)createTableViewDis
{
    tableViewDis = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, self.frame.size.width, self.frame.size.height-50) style:UITableViewStylePlain];
    
    tableViewDis.backgroundColor = [UIColor clearColor];
    
    tableViewDis.dataSource = self;
    tableViewDis.delegate = self;
    
    [self addSubview:tableViewDis];
}

//刷新数据
-(void)freshenViewDis
{
    [tableViewDis reloadData];
}

#pragma mark - tableView dataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    
    return 3+[modelIns.arrOutCheck count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    mm_outCheckDisUserDefinedCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (nil == cell)
    {
        cell = [[mm_outCheckDisUserDefinedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    //cell的风格是：浅浅灰色和浅灰色间隔出现，第一行是浅灰色
    NSInteger row = [indexPath row];
    switch (row%2)
    {
        case 0:
            //浅灰色
            cell.contentView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
            break;
        case 1:
            //浅浅灰色
            cell.contentView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
            break;
            
        default:
            break;
    }
    
    //给cell上设置数据
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    
    if (row<[modelIns.arrOutCheck count])
    {
        for (int i = 0; i<[cell.arrForAllKeys count]; i++)
        {
            NSString *strKey = [cell.arrForAllKeys objectAtIndex:i];
            NSString *txt = nil;
            if ([strKey isEqualToString:MM_OUT_CHECK_DEFECT_COUNT_KEY_STR_]) {
                txt = [[[modelIns.arrOutCheck objectAtIndex:row] objectForKey:strKey]stringValue];
            }else{
                txt = [[modelIns.arrOutCheck objectAtIndex:row] objectForKey:strKey];
            }
            
            ((UILabel *)[cell.dicAllLblskOnCell objectForKey:strKey]).text = txt;
        }
    }
    else
    {
        for (int i = 0; i<[cell.arrForAllKeys count]; i++)
        {
            NSString *strKey = [cell.arrForAllKeys objectAtIndex:i];
            ((UILabel *)[cell.dicAllLblskOnCell objectForKey:strKey]).text = @"";
        }
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

#pragma mark - tableView delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    
    if (row<[modelIns.arrOutCheck count])
    {
        //响应点击
    }
    else
    {
        return;
    }
}

@end
